package com.sosee.cgfw.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.sosee.cgfw.R;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Thinkpad on 2016/3/3.
 * 意见反馈界面
 */
public class FeedbackActivity extends Activity {
    private ImageView ig_miaoshu;
    private EditText et_miaoshu;
    private EditText et_phone;
    private ImageView ig_add;
    private BitmapUtils bitmapUtils;
    private String miaoshu;
    private String phone;
    private HttpUtils httpUtils;

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initView();
    }
    public void initView(){
        bitmapUtils=new BitmapUtils(this);
        httpUtils=new HttpUtils();
        httpUtils.configCurrentHttpCacheExpiry(1000);
        et_miaoshu= (EditText) findViewById(R.id.et_miaoshu);
        et_phone= (EditText) findViewById(R.id.et_phone);
        ig_add= (ImageView) findViewById(R.id.ig_add);
    }


    //点击事件
    public void onClick(View view){
        int id=view.getId();
        switch (id){
            //添加图片
            case R.id.ig_add:
                getPic();
                break;
            //提交意见反馈
            case R.id.rl_commit:
                commit();
                break;
            //返回键
            case R.id.rl_back:
                back();
                break;
        }
    }
    //返回
    public void back(){
        finish();
    }


    //打开系统相册  "image/*"
    private int RESULT_LOAD_IMAGE=0;
    public void getPic(){
        Intent intent = new Intent(
                Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, RESULT_LOAD_IMAGE);

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            LogUtils.log_E("图片路劲：==" + picturePath);


            ImageView imageView = (ImageView) findViewById(R.id.ig_img);
            Bitmap bit=BitmapFactory.decodeFile(picturePath);
            imgUrl=picturePath;
            bit=small(bit);
            imageView.setImageDrawable(resizeImage(bit,Utils.dip2px(this,350),Utils.dip2px(this,350)));
        }
        super.onActivityResult(requestCode, resultCode, data);
    }







    //压缩图片
    private static Bitmap small(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.postScale(0.5f,0.5f); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
        return resizeBmp;
    }
    //指定大小对图片进行压缩

    public static Drawable resizeImage(Bitmap bitmap, int w, int h)
    {
        Bitmap BitmapOrg = bitmap;
        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = w;
        int newHeight = h;

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        float n=scaleHeight>scaleWidth?scaleHeight:scaleWidth;

        Matrix matrix = new Matrix();
        matrix.postScale(n, n);
        // if you want to rotate the Bitmap
        // matrix.postRotate(45);
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                height, matrix, true);
        return new BitmapDrawable(resizedBitmap);
    }


    //提交意见反馈
    private String imgUrl;
    public void commit(){
        phone=et_phone.getText().toString();
        miaoshu=et_miaoshu.getText().toString();
        if(miaoshu==null||miaoshu.trim().length()<1||phone==null||phone.trim().length()<11){
            Utils.showToast("请输入问题和联系方式");
            return;
        }
        LogUtils.log_E(Constans.FEEDBACK_URL+phone+"&feedback"+miaoshu);
        httpUtils.send(HttpRequest.HttpMethod.GET, Constans.FEEDBACK_URL + phone + "&feedback" + miaoshu
//                ,params
                , new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject jsonObject=new JSONObject(responseInfo.result);
                    //log  json

                    LogUtils.log_E(responseInfo.result);
                    String status=jsonObject.getString("status");
                    if("success".equals(status)){
                        finish();
                    }else{
                        Utils.showToast("上传失败");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Utils.showToast(getString(R.string.internet_error));
            }
        });

    }


}
